package com.dev.dream.libs.preperences;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.dev.dream.libs.mylib.R;

public class IconPreference extends Preference {

	private Drawable icon = null;

	public IconPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setLayoutResource(R.layout.preference_icon);

		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.IconPreference, defStyle, 0);
		icon = ta.getDrawable(R.styleable.IconPreference_icon);
	}

	public IconPreference(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	protected void onBindView(View view) {
		super.onBindView(view);

		ImageView imageView = (ImageView) view.findViewById(R.id.icon);
		if (imageView != null) {
			if (icon != null) {
				imageView.setImageDrawable(icon);
			} else {
				imageView.setVisibility(View.GONE);
			}
		}
	}

	public void setIcon(Drawable icon) {
		if (this.icon == null && icon != null || icon != null
				&& !icon.equals(this.icon)) {
			this.icon = icon;
			notifyChanged();
		}
	}
}
